const dbConfig = require('./db.js')
const mysql = require('mysql')
const sql = require('./sql.js')
const express = require('express')
const bodyParser = require('body-parser')
var app = express()

app.use(bodyParser());

var connection = mysql.createConnection(dbConfig)
connection.connect();

app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});
// 登录
app.post('/api/login', (req, res, next) => {
  let a = req.body;
  let user = [a.username, a.password, a.level]
  connection.query(sql.user.login, user, function(err, result) {
    if(err) {
      console.log(err);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  });

})
// 用户名是否存在
app.post('/api/username', (req, res, next) => {
  let username = req.body.username;

  connection.query(sql.user.queryUsername, username, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500)
    }
    res.send(result);
  })
})
// 注册
app.post('/api/register', (req, res, next) => {
  let a = req.body;
  let register_user = [a.username, a.password, a.name | '', a.answer, a.mail | ''];
  connection.query(sql.user.register, register_user, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
  })
  res.sendStatus(200);
  next();
})
// 得到全部版块
app.get('/api/queryForum', (req, res, next) => {
  connection.query(sql.main.queryForum, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 得到版块下的全部帖子
app.get('/api/queryPost', (req, res, next) => {
  let id = req.query.id;
  ids = [id, id];
  connection.query(sql.main.queryPost, ids, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 得到帖子
app.get('/api/Post', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.main.post, id, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 得到帖子下的评论
app.get('/api/comment', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.main.comment, id, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }

    res.status(200).send(result);
  })
})
// 发表帖子
app.post('/api/sendPost', (req, res, next) => {
  let a = req.body;
  let post = [a.postTitle, a.postContent, a.postTime, a.postUserId, a.postForumId];
  connection.query(sql.main.sendPost, post, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 发表评论
app.post('/api/sendComment', (req, res, next) => {
  let a = req.body;
  let comment = [a.content, a.commTime, a.commUserId, a.commPostId];

  connection.query(sql.main.sendComment, comment, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 得到用户
app.get('/api/userInfo', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.user.userInfo, id, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 得到用户帖子
app.get('/api/userPost', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.user.userPost, id, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 得到用户评论
app.get('/api/userComment', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.user.userComment, id, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 保存修改信息
app.post('/api/userInfoSave', (req, res, next) => {
  let a = req.body;
  let info = [a.name, a.mail, a.id];
  connection.query(sql.user.userInfoSave, info, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
  })
  res.sendStatus(200);
})
// 用户删除帖子
app.get('/api/deleteUserPost', (req, res, next) => {
  let id = parseInt(req.query.id);

  connection.query(sql.user.deletePost, id, (error, result, fields) => {
    if(error) {
      console.log(error);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 用户删除评论
app.get('/api/deleteUserComment', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.user.deleteComm, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 版主版块
app.get('/api/getForum', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.moderator.queryForum, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 置顶的帖子
app.get('/api/queryStick', (req, res, next) => {
  let forumid = req.query.id;

  connection.query(sql.moderator.queryStick, forumid, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }

    res.status(200).send(result);
  })
})
// 删除帖子
app.get('/api/deletePost', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.moderator.deletePost, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 置顶帖子
app.get('/api/stickPost', (req, res, next) => {
  let id = req.query.id;
  // 将信息插入stick表
  connection.query(sql.moderator.stickPost, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);

  })
})
// 取消置顶
app.get('/api/deleteStick', (req, res, next) => {
  let id = req.query.id;
  // 将信息从stick表删除
  connection.query(sql.moderator.deleteStickThen, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 申请版主
app.post('/api/apply', (req, res, next) => {
  let a = req.body;
  let apply = [a.forumName, a.forumContent, a.applyReason, a.applyTime, a.userId]
  connection.query(sql.user.apply, apply, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 查看申请
app.get('/api/getApply', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.user.checkApply, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 查看全部用户
app.get('/api/getAllUserP', (req, res, next) => {
  connection.query(sql.admin.queryAllUserP, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
app.get('/api/getAllUserC', (req, res, next) => {
  connection.query(sql.admin.queryAllUserC, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 删除用户
app.get('/api/deleteUser', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.admin.deleteUser, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 查看全部申请
app.get('/api/getAllApply', (req, res, next) => {
  connection.query(sql.admin.queryAllApply, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 同意申请
app.get('/api/acceptApply', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.admin.acceptApply, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 拒绝申请
app.get('/api/rejectApply', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.admin.rejectApply, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 查看全部版块信息
app.get('/api/getAllForum', (req, res, next) => {
  connection.query(sql.admin.queryAllForum, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
// 删除版块
app.get('/api/deleteForum', (req, res, next) => {
  let id = req.query.id;
  connection.query(sql.admin.deleteForum, id, (error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.sendStatus(200);
  })
})
// 查询系统信息
app.get('/api/getInfo',(req, res, next) => {
	let date1 = req.query.date1;
  connection.query(sql.admin.queryInfo, [date1,date1,date1],(error, result, fields) => {
    if(error) {
      console.log(error.messagr);
      return res.sendStatus(500);
    }
    res.status(200).send(result);
  })
})
app.listen(3000);
console.log('success listen at port:3000......');